package org.dashboard.client.widget.backgroundimage;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Node;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.*;
import com.google.gwt.user.client.ui.Widget;
import org.dashboard.client.widget.ImageWidget;

/**
 * @author Simon Vogensen, Lakeside
 * @version $Revision$
 */
public class BackgroundImageWidget extends ImageWidget {

    interface Binder extends UiBinder<Widget, BackgroundImageWidget> { }
    private static final Binder binder = GWT.create(Binder.class);

    public BackgroundImageWidget(final String[] urls, int changeInterval) {
        super();
        setWidget(binder.createAndBindUi(this));

        Node node = getElement().getChild(0);
        DOM.setStyleAttribute((Element) node, "width", (Window.getClientWidth()-30)+"px");
        DOM.setStyleAttribute((Element) node, "height", (Window.getClientHeight()-30)+"px");

        final Timer timer = new Timer() {
            @Override
            public void run() {
                replaceImage(urls[Random.nextInt(urls.length)]);
            }
        };
        timer.scheduleRepeating(changeInterval);
        replaceImage(urls[Random.nextInt(urls.length)]);
    }
}
